import face_recognition
import time
import uuid
import os
import requests
from paddleocr import PaddleOCR

class ImgUtils():
    
    def getRandomNameAndCreatFolder(self,name,type):
        folder = self.getAndCreatFolder(type)
        return folder + str(uuid.uuid4())+"."+ self.getFileType(name)
    
    def getAndCreatFolder(self,type):
        folder = "imgs/" + type +"/" + self.getCurrentDay() + "/"
        if not os.path.exists(folder):
            os.makedirs(folder)
        return folder
    
    def getFileName4path(self,path):
       return path.rsplit('/', 1)[1].lower()
    
    def getFileType(self,filename):
        return filename.rsplit('.', 1)[1].lower()
    
    def getCurrentDay(self):
        return time.strftime("%Y_%m_%d")

    def saveImg(self,mfile,type = "FACE"):
        filePath = self.getRandomNameAndCreatFolder(mfile.filename,type)
        mfile.save(filePath)
        return filePath

    def getFaceInfo(self,file):
        img = face_recognition.load_image_file(file)
        face_encodings = face_recognition.face_encodings(img)
        return face_encodings
    
    def downImgae4url(self,url,type = "FACE"):
        reponse = requests.get(url,timeout=10)
        if reponse.status_code == 200:
            local_img_url = self.getRandomNameAndCreatFolder(url,type)
            with open(local_img_url,'wb') as f:
                f.write(reponse.content)
                return local_img_url
        return None
    
    def compareFace(self,imgJsonInfo,file):  
        pass